﻿using pHMb.Spectrogram;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Lung_Sounds_V2
{
    /// <summary>
    /// Interaction logic for SelectionsWindow.xaml
    /// </summary>
    public partial class SelectionsWindow : Window
    {
        public SpectrogramPane Spectrogram { get; set; }
        public double FftWindowSize { get; set; }

        public string WavFileName { get; set; }

        public SelectionsWindow(SpectrogramPane spectrogram, double windowSize)
        {
            Spectrogram = spectrogram;
            FftWindowSize = windowSize;
            InitializeComponent();
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            //LoadSelections();
        }

        private void listBoxSelections_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DeleteSelected();
        }

        private void DeleteSelected()
        {
            if (this.listBoxSelections.SelectedIndex != -1)
            {
                var selections = listBoxSelections.SelectedItems;

                List<SpectrogramPane.XSelection> removedSels = new List<SpectrogramPane.XSelection>();

                foreach  (SpectrogramPane.XSelection selection in selections)
                {
                    removedSels.Add(selection);
                }

                foreach (SpectrogramPane.XSelection selection in removedSels)
                {
                    Spectrogram.SelectedXPoints.Remove(selection);
                }
            }
        }

        private void ButtonAvgFFT_Click(object sender, RoutedEventArgs e)
        {
            Dictionary<string, List<double[]>> snippets = new Dictionary<string, List<double[]>>();

            // Performs analysis to find average frequency data
            if (Spectrogram.SelectedXPoints.Count > 1 && 
                MessageBox.Show("Display all on same graph?", "Average FTT", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                foreach (pHMb.Spectrogram.SpectrogramPane.XSelection selection in Spectrogram.SelectedXPoints)
                {
                    snippets.Add(selection.ToString(), selection.GetWindows());
                }

                new GraphWindow(snippets, "Average FFT", "Frequency (Hz)", "Amplitude (dB)", (1.00 /  FftWindowSize)).Show();
            }
            else
            {
                foreach (pHMb.Spectrogram.SpectrogramPane.XSelection selection in Spectrogram.SelectedXPoints)
                {
                    new GraphWindow(new Dictionary<string, List<double[]>>() { { selection.ToString(), selection.GetWindows() } }, "Average FFT", "Frequency (Hz)", "Amplitude (dB)", (1.00 / FftWindowSize)).Show();
                }

            }
        }

        private void Button_Neuralise_Click(object sender, RoutedEventArgs e)
        {
            List<List<double[]>> snippets = new List<List<double[]>>();
            foreach (pHMb.Spectrogram.SpectrogramPane.XSelection selection in Spectrogram.SelectedXPoints)
            {
                snippets.Add(selection.GetWindows());
            }

            new AnalyseWindow(snippets).ShowDialog();
        }

        private static readonly double[] _avgLung = new double[] { -10.10962554, -10.38972404, -11.33315059, -11.26633474, -10.23153005, -10.08054993, -8.876216077, -8.429019251, -6.449231221, -5.768223472, -5.844488127, -4.080887517, -2.779946404, -2.20576944, -1.140817518, 0.204600227, 0.837572565, 0.507194603, 0.768756439, 1.870346866, 2.896368433, 3.558429253, 4.13432777, 4.299249317, 4.737301113, 5.74031604, 6.447938323, 6.901127663, 6.541224005, 6.920857231, 6.353438976, 8.169542107, 8.091602036, 8.938168219, 9.510182285, 9.958168227, 8.819137623, 6.991224548, 8.30019676, 9.431619902, 9.639376233, 7.183124925, 6.075784751, 8.863538313, 8.884164952, 7.725152642, 3.50619932, 4.476863144, 7.331939551, 7.000715703, 6.108015269, 4.50529461, 4.869221518, 5.949642512, 5.886565944, 3.279995378, 2.254563981, 3.256694973, 3.908274628, 3.682852264, 2.157252015, 1.722168917, 1.685756424, 1.907493171, 0.71694473, -0.636735828, -0.316938944, 0.24441976, 0.581963991, -1.463151521, -2.436906331, -0.529832701, -1.170463208, -2.187888887, -3.725240409, -3.339881987, -2.328851907, -1.655228774, -2.584697485, -3.702760596, -2.833984635, -3.282501552, -3.662842649, -5.463738405, -6.174825472, -5.647672496, -4.231749065, -4.119058122, -4.845538812, -4.83499495, -4.736848713, -4.07865077, -4.178807745, -5.131649303, -4.586230934, -3.94668627, -4.032306324, -4.201390047, -4.469625273, -4.362719324, -3.970091009, -4.52383416, -5.723677439, -5.226066799, -4.96811458, -4.504117871, -3.816831523, -4.567418112, -3.578371221, -3.835118964, -3.577988151, -5.016644929, -5.241308441, -5.051425968, -5.018446346, -4.892997832, -4.897877002, -4.616455284, -4.578864072, -4.535188751, -4.760933571, -4.150384277, -4.069455226, -4.014161992, -4.166331006, -4.554887707, -5.366331541, -5.177808527, -4.119213277, -4.325838153, -4.69689159, -4.236405945, -4.408625255, -3.802033266, -4.181735525, -5.310079687, -5.497084882, -3.917583696, -4.580967008, -3.755684132, -4.051559541, -3.364392916, -4.531567935, -4.203177495, -3.333616337, -3.833971869, -4.312794581, -4.486186885, -4.128470226, -3.85415899, -3.512033064, -3.686866924, -4.693807476, -4.504272286, -5.208389017, -4.989747139, -4.29459972, -4.576091086, -5.454642345, -5.950491064, -6.753533671, -7.338828675, -7.236855303, -7.690971851, -7.250825212, -7.815139402, -7.441744464, -7.615792343, -7.544616557, -7.531800744, -7.347897456, -8.249675533, -8.613950971, -8.972892698, -8.018320896, -7.71559721, -8.680340974, -8.736054902, -8.664226624, -8.665173397, -8.676151681, -9.53988919, -9.865452897, -8.977806441, -9.210513, -9.321275107, -9.982979024, -10.36421263, -9.996096147, -9.858083088, -10.39012534, -10.27901211, -9.933538309, -9.994625685, -10.51997755, -10.81909603, -11.02424994, -11.14531328, -10.92637102, -11.60565589, -11.46960772, -10.31783271, -10.30364804, -10.81785663, -10.42926145, -10.43232984, -11.46487487, -11.0725594, -11.39472158, -10.92513854, -11.77717911, -11.51711628, -11.16241915, -11.16911662, -11.69132304, -11.01828488, -11.1516392, -11.68706754, -11.78530004, -11.5588517, -11.57187232, -11.67518823, -11.38303988, -11.74520602, -11.46327316, -11.32923909, -11.3395771, -11.88760555, -12.61935278, -11.98363055, -11.24921089, -11.16006419, -10.71635162, -11.69297561, -12.25166115, -12.45553986, -11.15119819, -11.73956778, -12.00484073, -12.23126787, -11.68841648, -11.08845048, -11.19102771, -11.93821222, -12.20499326, -11.13315386, -11.08782434, -11.70447877, -12.42968779, -11.46699666, -11.52387925, -11.75010692, -11.97565139, -12.24293446, -11.87174402, -12.05311043, -12.47437896, -12.30422919, -11.26340456, -11.24832285, -11.77176334, -11.36696239, -10.53212184, -10.92819236, -11.16401299, -11.10833244, -11.51527662, -11.29603201, -11.55497141, -11.64446076, -11.43393419, -11.89819468, -11.9738601, -12.05197259, -11.50710468, -11.6269431, -11.74288572, -11.87352944, -11.21982618, -11.55702645, -12.6644435, -12.01595169, -11.73067464, -11.60478329, -11.78147015, -12.07891965, -12.04366105, -11.4513901, -11.11804468, -11.53520308, -12.3797769, -12.14719118, -11.80822827, -12.12635254, -11.58345378, -11.19108861, -11.66506161, -11.57188031, -12.48258548, -12.59759289, -11.85972058, -12.45887867, -12.64266467, -12.02989322, -10.9233726, -10.74459096, -10.35565051, -11.85429726, -11.4298401, -11.36345927, -11.28403199, -11.3806435, -11.28072856, -11.44484055, -11.50740174, -11.55178545, -11.14556709, -10.97376428, -11.64880687, -11.07664962, -11.60924096, -11.0431354, -11.74492504, -12.28370704, -12.28073763, -12.11018657, -11.58463313, -11.69261817, -12.32806811, -12.24889987, -11.79104875, -11.97480975, -11.78392092, -11.23774009, -11.84788751, -12.43390147, -11.61336164, -12.38306895, -12.37899594, -12.05106846, -11.92246888, -12.14129675, -11.50567704, -12.33651814, -12.02970148, -11.85110931, -11.23897096, -11.7431732, -11.5115192, -12.40803707, -11.82755776, -11.14809976, -11.56381292, -12.31414126, -11.8371553, -11.27855695, -11.76017102, -12.12582775, -12.44100235, -12.03844172, -11.50604627, -11.58509242, -11.52106222, -11.51234582, -11.74257023, -11.50399183, -11.5927714, -12.05340834, -11.84063522, -11.34007855, -11.45240433, -11.88612047, -11.88561766, -11.85637281, -12.17025726, -11.98263284, -11.8847898, -11.82182776, -11.36611122, -11.85454509, -12.05448412, -11.56240115, -11.48725994, -12.64935429, -12.34613516, -11.22050119, -11.65505622, -11.89328683, -11.51520719, -11.37134751, -11.70644202, -11.51118583, -11.42205398, -11.45209821, -11.80727846, -11.55424191, -11.24480295, -10.93711406, -12.00925083, -12.1035415, -11.87994755, -11.21138904, -11.85103225, -11.52453778, -11.49629442, -12.22917302, -11.87872026, -12.76918484, -11.80084834, -12.12885554, -11.45205095, -11.91268588, -10.82533031, -11.8443416, -11.63740268, -11.42787269, -11.82903644, -11.54431805, -11.16083485, -10.63011371, -11.62783564, -11.31468954, -10.88859508, -11.05470914, -12.05349415, -11.77859368, -11.7009979, -11.6924186, -10.95195822, -11.60760398, -12.13483495, -11.78902899, -11.09780438, -12.33374781, -11.52994601, -11.27627044, -11.38564954, -11.73733841, -11.69241351, -11.4769472, -12.03136492, -12.22660736, -11.73746705, -12.08449536, -11.18336747, -11.73657053, -12.28445253, -11.50004106, -11.21986553, -10.57346039, -11.5375225, -12.11861302, -12.05953546, -11.95164176, -10.57164722, -11.50273733, -12.19542385, -11.12268423, -11.2181675, -11.46147281, -11.85226955, -11.81677204, -12.2269689, -11.63839717, -11.27186597, -11.74231225, -12.15779447, -11.31957278, -10.58666142, -11.73855928, -11.66564829, -11.84168102, -11.54835212, -11.53862704, -12.21776697, -11.7334663, -11.34171813, -11.33611865, -12.30193838, -12.26871104, -11.59452302, -11.80369616, -11.55751843, -11.67727219, -11.89414064, -11.9191415, -12.08443106, -11.09328498, -11.19240297, -11.80301661, -12.61622701, -11.87125794, -11.48587109, -11.49633433, -12.41362267, -11.97762335, -11.82704795, -11.03151674, -11.45191834, -12.26271756, -12.41282211, -11.97351168, -11.72000013, -11.14943712, -11.7961344, -11.4017809, -12.27213502, -12.06368117, -12.2422665, -12.11514659, -11.83797119, -11.96351141, -12.11904298, -11.33602598, -10.87816289, -11.49960138, -11.12740843, -12.06244544, -11.99922696, -11.69521629, -11.81117072, -11.55527749, -11.73144827, -11.71641452, -11.90831493, -11.68380169, -11.34112, -11.52600049, -11.53619503, -11.27909151, -11.23657711, -11.71377277, -11.87390571, -12.03762641, -11.50298582, -11.85472291, -11.24477443, -11.37285146, -12.02497435, -11.71456361, -11.28799585, -11.74392409, -11.74893842, -11.71935858, -11.91263212, -11.88049729, -11.3609429, -12.29599316, -11.86339882, -11.59325099, -12.23966013, -12.11951115, -10.80177245, -10.62138928, -11.80433373, -11.92113022, -11.65035734, -12.60027579, -11.43560085, -12.48261849, -11.47986889, -11.84222751, -12.09200253, -12.56960919, -12.16140491, -11.81143201, -11.3481988, -11.07810005, -11.12812739, -12.21419597, -11.51813708, -11.44965198, -12.06405669, -11.5147429, -11.4538233, -11.41473313, -11.91851622, -11.62119434, -11.25847651, -11.37784806, -11.50558564, -11.40774706, -12.01183171, -12.57179307, -11.66214026, -11.57406787, -11.57464487, -11.21372297, -11.79619002, -11.19435021, -11.6338319, -11.80118912, -12.33752582, -11.59345947, -11.79859774, -11.44494515, -11.61447408, -11.22322186, -11.17571471, -12.49854445, -11.69466444, -11.46366591, -11.35656455, -11.5182722, -10.94330807, -11.20629899, -11.80345815, -11.72172, -11.31568828, -11.16096091, -12.15569575, -12.30143242, -11.37162408, -11.63654123, -12.51243415, -12.58493664, -12.88477676, -12.59987055, -12.53470008, -11.73780054, -11.89973411, -12.00903782, -11.65958991, -11.63195617, -12.39881669, -11.64871946, -11.1658823, -12.14092364, -12.14527341, -12.60275075, -11.73940041, -11.35309513, -10.98480338, -11.71723031, -12.02452166, -12.15847418, -12.16656741, -12.540433, -11.68029063, -11.85977059, -11.98806889, -11.54324576, -11.42158201, -11.71408569, -11.74505167, -11.65196384, -11.64204614, -11.75246548, -11.97005344, -11.17659098, -11.98206974, -11.85319369, -12.30126577, -12.02967455, -11.93711471, -12.00015445, -12.18517082, -11.22587245, -11.04771864, -11.39131422, -12.23927242, -12.4871692, -12.37557663, -12.0038091, -11.67224125, -12.23529075, -11.64554374, -11.85564822, -11.58775004, -11.38345313, -12.43965821, -11.81978844, -12.15626503, -12.41540963, -11.61534596, -11.03886217, -10.92461717, -10.98915562, -11.5855407, -12.05887885, -11.23981737, -11.27481025, -11.89231363, -11.98707166, -12.09099616, -11.85402889, -11.82064372, -11.24333951, -11.43364464, -11.43075552, -11.88151284, -12.07346044, -11.54020935, -11.89021648, -11.49834119, -11.84557317, -11.72054913, -11.53998754, -11.69860169, -11.46633271, -11.72228748, -11.76317946, -11.89682128, -11.39735488, -11.46754532, -11.92285355, -12.41675594, -12.71956708, -11.7876115, -11.8021862, -11.79675479, -11.85818313, -11.74292315, -12.28710133, -11.21133701, -12.35756438, -12.06526737, -11.82383934, -12.09878145, -11.57284989, -12.39195872, -12.83883035, -11.62736168, -11.56744483, -11.31744004, -10.42110528, -11.60112371, -11.25038518, -11.83132822, -11.52988896, -11.54163731, -10.92432468, -11.59011501, -11.98358046, -11.52769747, -11.5148727, -11.03940537, -11.3416895, -12.18865668, -12.39583152, -12.22518539, -11.51199334, -10.94231864, -11.86989146, -11.36346112, -11.33896648, -11.06998394, -11.27430283, -11.6968517, -11.65850032, -12.13279678, -11.57456652, -10.85685733, -11.5958196, -11.36785582, -11.62256123, -11.78205747, -12.3642422, -11.94151437, -11.84501807, -10.815418, -10.85074029, -11.90818938, -11.62047304, -11.6561416, -11.6028609, -11.37960802, -11.47906955, -12.09261666, -11.9600648, -11.8625678, -12.04274935, -11.93852453, -11.69831469, -11.84091736, -11.90973409, -11.95878331, -12.10914049, -11.41294472, -12.08773671, -11.46771281, -11.72199446, -12.11174318, -11.16003958, -11.39641503, -12.52849269, -11.47645186, -12.02662081, -12.08660214, -11.37857236, -10.66873628, -11.26455855, -11.68722965, -11.57503325, -11.21072834, -12.20732555, -12.28980504, -12.16255314, -11.72410211, -11.40492193, -11.57831177, -11.49621509, -11.49769147, -11.79342111, -11.63728933, -11.86716669, -11.84560396, -11.7460922, -12.28225036, -12.03268226, -11.7551471, -12.24246381, -12.0702264, -11.33249236, -11.43939342, -11.00087115, -11.91117487, -12.09724948, -12.37817486, -12.17317736, -12.1348664, -12.44140816, -11.36300712, -11.87509868, -11.20177954, -11.55715178, -11.07517754, -11.80727214, -11.61231409, -11.75500089, -11.46285962, -11.91188377, -12.54925549, -11.70333148, -11.94408547, -11.9636358, -12.13659319, -12.07416916, -12.54806583, -11.70199867, -11.66854819, -11.07898585, -11.73617503, -10.37781225, -11.32529371, -11.38420051, -11.11910557, -11.48558139, -11.24747149, -10.77963649, -12.152547, -11.75405867, -11.99941711, -11.77641677, -11.20534028, -12.29126559, -11.96808926, -11.47158567, -11.24468046, -10.61514392, -10.91451436, -11.31273552, -11.33432498, -11.46568351, -12.37186889, -12.21944215, -12.15112133, -12.00905934, -11.2564698, -11.45310034, -11.46266474, -11.74549311, -10.51666578, -10.71607698, -11.71621907, -12.42901421, -12.14113141, -11.94138387, -11.35398699, -11.7377674, -11.25269099, -11.87283838, -12.014381, -11.71405387, -12.2169419, -12.05393354, -12.17403352, -12.38603493, -11.40153843, -11.64819528, -12.49233391, -12.21151278, -12.15721135, -11.10137341, -11.19755911, -12.24839095, -12.36869724, -12.21478584, -11.04466835, -11.60733035, -11.56354454, -12.2264894, -11.46620959, -11.60937807, -11.99939712, -11.75840313, -11.91641004, -11.58787032, -12.37634646, -11.79680017, -12.45789455, -11.80366262, -12.23337603, -11.58193343, -11.14060452, -12.02551078, -12.08815766, -12.15523778, -11.26002932, -11.59188641, -11.65387705, -11.47011164, -11.75636649, -11.74047862, -12.5318076, -11.77421578, -11.74767733, -11.99022847, -11.65203521, -11.964661, -11.64941883, -10.80090417, -11.39302592, -11.58477261, -12.18485171, -11.98432889, -11.06510828, -12.02858404, -12.04078714, -11.5889193, -11.44320232, -11.35699127, -11.84255566, -11.70009046, -11.27064635, -11.47847912, -11.08212258, -11.86636231, -12.0635749, -11.57352018, -10.82050691, -10.90177497, -11.44023063, -11.18311496, -11.81260247, -11.72738576, -12.12074795, -12.21489089, -11.06214435, -11.76069421, -12.05460133, -11.7833966, -11.37727114, -11.38924783, -11.15407317, -12.04836967, -12.19498889, -12.16488612, -11.85238175, -12.12507174, -12.0571237, -11.34636378, -10.98394911, -11.47894615, -11.68665698, -11.06750398, -11.46911696, -12.2412716, -11.94462918, -11.21660905, -11.47963247, -10.6563997, -11.57697295, -11.59047928, -12.35565073, -11.35026106, -11.39398993, -11.97308399, -11.92527453, -11.3634765, -10.871852, -11.82898606, -11.43247127, -12.12408791, -12.05718588, -12.30396035, -12.12887283, -12.12485416, -11.73587418, -11.32360007, -11.54444876, -11.13662757, -11.04300627, -11.48658672, -11.15323522, -11.27942512, -11.75169418, -11.95124286, -11.70135398, -11.57554629, -11.84110289, -11.34482116, -11.16456792, -11.45028584, -11.90057759, -11.45819049, -11.15684471, -11.33459248, -11.74643364, -11.44562599, -11.63271767, -11.53666218, -11.2543434, -12.07451002, -11.95437514, -11.88126078, -11.17782513, -11.11891267, -11.94693341, -11.6999928, -11.85393344, -11.74129913, -11.46765463, -11.14060893, -11.79239288, -11.34914469, -11.67847327, -11.5278615, -11.42287561, -12.17499318, -12.15658397, -11.93138558, -11.67914791, -11.84163746, -11.86038237, -12.03976748, -11.44265239, -11.13328354, -11.51497457, -11.54672994, -11.68165145, -11.57238841, -11.20609088, -10.49739066, -11.00848218, -11.33286396, -11.33380815, -12.0861502, -12.12177707, -11.02894619, -11.42180269, -11.92532067, -11.89987654, -11.21406633, -11.88549339, -11.7101161, -11.68326394, -11.39031231, -11.3249387, -12.1954825, -11.47820343, -11.01627603, -11.8925919, -11.89783965, -11.86316747, -11.8586764, -12.13180967, -11.95809627, -11.1944448, -12.35078877, -12.4261038, -12.01130981, -11.94365292, -11.696502, -11.07469764, -11.60348512, -12.0213416, -11.68979572, -11.39602041, -11.64316405, -11.24411659, -11.53360937, -11.34407072, -11.59857654, -11.89283455, -11.80288977, -12.07263427, -12.15154003, -11.75308239, -11.99619851, -11.69809558, -11.82893096, -12.07010723, -12.15254104, -11.6083378, -11.73966776, -11.70185402, -11.53045483, -11.83733908, -11.54949755, -11.5505347, -12.10662773, -12.30215266, -11.60633516, -11.92001565, -12.01472464, -11.6862734, -11.29094258, -11.21097911, -11.53519266, -12.25169151, -11.68484522, -12.30361345, -11.97126786, -11.90402863, -11.22385119, -11.90769013, -11.74225448, -10.96275199, -11.6630333, -11.97584134, -12.28817641, -12.11257096, -10.79309006, -11.58553484, -11.765064, -11.17428215, -12.16891911, -12.03149905, -12.2033728, -11.6864733, -11.52242224, -11.93045631, -11.99778493, -10.98573962, -11.91846916, -11.95410176, -11.85912083, -11.50764206, -11.25458705, -11.95000267, -11.31634752, -11.52322837, -11.76143319, -12.04379602, -11.88217364, -11.97572479, -12.48316853, -12.30370868, -12.15072725, -11.37162667, -11.36585444, -11.63809799, -11.77506933, -12.21317947, -11.70352962, -11.5725534, -12.41437273, -12.17160636, -11.87681917, -11.74647134, -11.51397759, -11.83026642, -11.27057383, -12.17499139, -12.18763398, -11.88615781, -12.4095443, -11.60898399, -11.36714196, -12.42054118, -11.81344409, -10.77272413, -10.02390876, -10.88994018, -12.02540447, -11.71895023, -10.7754441, -11.31486121, -11.85842648, -12.34798091, -11.36406771, -11.79439771, -11.48725515, -11.11388746, -10.64137744, -11.60664458, -11.28761627, -11.85746142, -11.63727103, -11.6222442, -11.96196286, -10.8156968, -11.58309742, -11.79555975, -11.59407026, -11.94689636, -11.56602552, -11.71286291, -11.19538989, -11.4068257, -11.24910393, -11.60712497, -12.33460982, -12.01137682, -11.26710478, -11.82694632, -11.44085144, -11.82118168, -11.53546718, -11.62888327, -11.75162151, -11.66005249, -11.69688926, -11.89515551, -11.71369554, -11.20124876, -11.60737177, -11.43543338, -11.45209388, -11.83708973, -11.49798185, -11.62736237, -11.71221597, -11.61905886, -11.8256113, -11.71527295, -11.51839291, -12.25433331, -12.49238411, -12.15771109, -11.61771808, -12.21875186, -12.69952512, -11.808624, -11.29592727, -11.45022497, -11.85041511, -11.40722579, -11.88039663, -12.26661257, -11.53113187, -12.00259578, -11.90801166, -11.8615112, -11.73439777, -11.63892699, -11.8742665, -11.96700012, -11.82922444, -11.95646497, -11.51621737, -11.32840479, -11.30235702, -11.46806576, -11.18151231, -11.27124004, -10.70255989, -11.56040683, -11.56555919, -12.02556396, -11.70756897, -11.08276733, -11.10040566, -12.41570635, -11.83224905, -11.51513527, -11.57112434, -11.75917556, -11.05473877, -11.07743657, -11.76621485, -11.45523654, -11.51936044, -11.51374771, -11.79731595, -12.32851538, -12.07398178, -12.18503257, -11.58090682, -12.1616831, -11.74145858, -11.05169633, -11.93149746, -12.00860516, -10.9074712, -11.5757369, -11.58788628, -11.85603972, -12.75900333, -12.27690866, -11.61101253, -11.49921562, -11.63285367, -11.09570329, -10.75223971, -12.11615473, -11.90829533, -11.69569666, -11.47169654, -11.99808404, -11.46833926, -11.10019, -11.20088886, -11.37262929, -11.54765246, -12.13858666, -11.36633157, -11.25648251, -11.15398295, -11.73820097, -11.51969954, -12.30158649, -12.77786073, -11.99850138, -11.70522464, -11.82310719, -12.27186781, -11.67363557, -11.63948282, -11.42469407, -11.7661944, -11.3101066, -11.92013981, -11.46689776, -10.98306839, -11.89354096, -11.63270002, -11.82161063, -11.87201507, -11.8407708, -11.91233883, -11.45908972, -11.74397159, -11.73414854, -11.47863577, -12.09290507, -11.96287349, -11.8373492, -12.0362345, -11.29873406, -11.92209484, -11.84972256, -11.28496108, -12.33948624, -11.52071177, -11.37257814, -11.65185294, -12.27684132, -12.11514712, -12.23575316, -12.06400023, -11.62690991, -10.91138692, -11.50061444, -11.51169624, -12.20013631, -11.88133457, -11.4746455, -11.5727685, -11.62252825, -11.7304371, -11.3067213, -11.22963242, -11.97119531, -12.02088278, -12.17866356, -12.12631608, -12.00625793, -12.36018377, -12.01543419, -12.07132289, -12.18013622, -11.26989669, -11.21354356, -11.49541115, -10.6418697, -11.61531904, -12.0729955, -11.93792526, -11.52775522, -11.64751622, -11.76245447, -12.09644454, -11.89545994, -11.49865957, -11.75503789, -11.66046826, -11.42277781, -11.2142034, -11.56058671, -11.36706446, -12.04846595, -11.47924313, -11.51238637, -11.53330925, -11.1889729, -11.95708302, -11.85183479, -12.04597698, -11.56757402, -12.17341202, -12.04183588, -12.10524224, -11.85956645, -12.14749596, -11.51969487, -11.07342571, -11.31378214, -11.65187899, -11.57833755, -11.38737036, -11.69584359, -10.88307681, -12.04294133, -11.81628123, -12.12016883, -11.86211239, -11.37463502, -11.83436409, -11.45043552, -11.85103771, -12.36035037, -11.77343893, -11.63808061, -11.26118188, -11.68023402, -11.78829589, -11.74144246, -11.44331974, -10.94317131, -11.60576525, -11.29014289, -10.88121961, -10.99538938, -11.6458312, -11.24729469, -11.50247443, -11.79640949, -11.49081818, -11.05032, -11.47360278, -11.45018786, -11.57001652, -11.86649038, -11.45987699, -11.51636802, -11.83307848, -11.74308093, -11.65286109, -11.79259532, -11.71944435, -11.45004958, -10.43088372, -10.52380446, -11.36094876, -11.32113429, -12.12675124, -11.71876365, -11.24352322, -11.38130393, -11.57956039, -11.37617889, -11.05976445, -11.89391404, -12.22363255, -12.50941946, -11.72039113, -11.08184574, -11.09296723, -11.35233488, -10.62922686, -11.68747149, -12.66479213, -11.80051241, -11.69019953, -11.78377569, -11.45136907, -11.61990467, -11.75302386, -11.30257528, -11.8222892, -11.87411574, -11.78514343, -12.02466917, -12.00263438, -11.20124296, -11.22480807, -11.79886643, -12.14623256, -11.56844514, -11.00517356, -11.34305727, -11.28603521, -11.33528454, -11.43301318, -11.59054752, -11.80474701, -11.01691789, -11.47126671, -11.50499672, -12.27843073, -11.7538593, -12.0326074, -11.44070732, -10.99863903, -11.91543888, -11.59270063, -11.86412688, -12.13756983, -11.7375537, -11.23345117, -11.33981651, -12.58706148, -12.51916642, -11.32717384, -11.68918845, -11.26787945, -11.657801, -11.94889102, -11.22916363, -11.17602848, -11.82654459, -11.98555523, -11.9294528, -11.37805773, -11.38863774, -11.52327424, -11.69522643, -11.73006996, -12.51781145, -11.73735651, -11.04587131, -11.73772696, -12.39233552, -11.44407143, -12.33287798, -12.3392331, -11.57639025, -10.89760797, -11.46259068, -11.56910364, -11.94668515, -11.82462117, -10.94143242, -11.01050151, -11.08166004, -11.85054502, -12.43133397, -11.53245554, -11.52142121, -11.84861907, -11.89407737, -11.62004896, -11.71990311, -11.53698703, -11.01719564, -11.3687708, -11.25777696, -11.3334746, -11.76657918, -11.51475282, -11.43450032, -12.05853056, -12.55629177, -11.64148889, -11.93195471, -11.74988243, -12.18750633, -11.9419454, -11.57313153, -12.27908833, -11.64581207, -12.38847077, -11.94168496, -11.36260862, -11.68701743, -12.11603574, -11.8116314, -11.08806325, -12.20442264, -12.1792984, -12.16624176, -11.53662727, -11.822278, -11.34813867, -11.25636072, -12.26626595, -11.55284088, -11.64718662, -11.71096597, -11.466644, -10.81557534, -11.53615984, -10.6951746, -12.22472312, -11.57837691, -11.43628025, -11.03756437, -11.57711955, -11.28869406, -10.80689824, -11.75601607, -11.94049621, -11.41732731, -11.61165072, -11.47738353, -11.74369469, -12.09498202, -11.19549247, -11.00778807, -10.84638279, -11.74920226, -11.8352949, -11.9783177, -11.65774721, -11.7216916, -11.30890684, -11.62747263, -12.0584469, -11.75971017, -11.5055963, -11.78405478, -11.57146387, -11.61670948, -12.32336325, -12.21591958, -12.16672363, -12.80071724, -12.63771931, -11.89562859, -11.37042221, -11.29023275, -11.07762811, -11.74377824, -12.34025762, -12.26420097, -12.40009922, -11.64072923, -11.26072206, -11.33636606, -12.023224, -12.1565588, -11.78424061, -11.41120061, -11.85607573, -11.69922629, -11.71061493, -11.48727542, -11.61978619, -12.06985977, -11.39065948, -11.76553432, -11.87223444, -11.8143234, -12.07692919, -11.8300423, -11.57291161, -10.50469528, -11.45137348, -12.29419335, -11.93593735, -11.91581788, -11.50276208, -11.56732921, -11.18589732, -11.12164528, -11.37904418, -12.13825489, -12.08743278, -11.45977668, -12.06383161, -11.86280641, -11.87278042, -11.16921993, -10.74949184, -11.12092804, -10.80120383, -11.75809474, -11.84313816, -11.59141469, -11.35151138, -11.88883987, -11.30667931, -12.1596842, -12.53648159, -12.18339793, -11.30100761, -11.73452022, -11.36766528, -12.3222648, -12.50976169, -12.66701241, -12.05540213, -11.40188351, -11.72034248, -12.18215305, -11.58624799, -11.2122004, -11.35677252, -11.3136623, -11.30657614, -11.95105373, -11.40361386, -10.92224695, -11.05360846, -10.29675954, -11.11336959, -11.63797071, -11.71749354, -11.56608123, -11.41865579, -11.25974072, -11.31347031, -12.16543319, -11.96502472, -11.6471981, -10.88595489, -10.80044948, -11.42124495, -11.98746346, -11.75271739, -11.65153927, -11.74243358, -11.46879963, -12.06283987, -11.40542306, -11.47120862, -11.44542719, -11.67902118, -11.79930177, -12.01173485, -11.82333104, -12.03218429, -12.0541547, -11.81673596, -11.69977722, -11.63963677, -11.93564191, -11.90128002, -12.08550784, -11.7141384, -10.64984445, -11.99958517, -11.49652172, -11.56755849, -12.22027036, -11.09445067, -11.2139897, -11.08804071, -11.29873717, -11.04358774, -11.56410347, -11.60384708, -12.05269135, -11.594203, -12.2576483, -11.45365945, -11.27098677, -11.43918, -11.86868452, -10.94609879, -10.82111084, -11.14527755, -11.26115349, -10.97472833, -12.0814516, -12.15113347, -12.13194834, -11.75342889, -12.00466964, -11.55092306, -11.58234623, -11.74866065, -12.6104827, -11.76186492, -11.47990609, -11.84879915, -11.7181519, -11.64973727, -11.27999988, -11.65510648, -11.36743874, -11.66881318, -11.84746249, -11.29849523, -12.53581899, -12.24270707, -11.80560703, -12.011501, -11.60866534, -11.70918319, -11.65204033, -11.47052715, -11.88343857, -11.5384254, -11.69572642, -11.60213109, -11.53592905, -12.02812223, -12.28646609, -11.98076136, -11.92217221, -11.3476081, -11.44784872, -11.74914087, -11.78359002, -11.30513216, -11.66513769, -12.16937766, -11.60355421, -11.76566106, -11.3659886, -11.59144628, -11.66843522, -12.17864046, -11.39400953, -11.58057332, -12.10466413, -11.85227586, -11.52229155, -11.76001972, -11.1090184, -12.0529218, -12.21703565, -12.28916032, -11.99530027, -11.89352172, -11.24386987, -11.66138271, -11.43588865, -11.72960952, -10.88163171, -10.87487816, -11.12586223, -12.27014938, -12.14774055, -12.00279244, -11.51748445, -11.98329788, -11.77101793, -11.40167607, -10.87134752, -11.12074611, -11.52872264, -11.18583502, -11.34614132, -11.77591753, -10.87125689, -11.54803777, -11.76873709, -11.69770444, -12.00149567, -11.9620759, -12.20396551, -11.21814112, -11.40564563, -11.01751367, -11.72903847, -11.55100439, -11.55352623, -12.07568112, -12.32451325, -11.48359948, -11.86788491, -11.45710636, -11.19455975, -11.74586414, -12.13931587, -12.61737122, -11.95782311, -12.15232975, -12.22909615, -11.73530202, -11.63210549, -11.71939706, -11.62772467, -11.67190642, -11.71944868, -11.50231176, -11.89458401, -11.10556008, -11.74410134, -11.43852264, -11.51697749, -11.55901561, -11.45995864, -11.27131741, -11.24468239, -11.41592458, -11.88181558, -11.92790661, -12.16626976, -12.77647089, -11.98279476, -11.66231359, -10.5805444, -11.29300182, -11.34038636, -11.51420358, -11.51994896, -11.47454388, -11.32186027, -11.85255311, -11.50687514, -11.45153444, -11.35514606, -11.70911435, -11.67282859, -11.42566004, -11.79056396, -11.99489485, -12.04360636, -12.69678474, -11.86685141, -11.55049633, -11.73189074, -11.13852652, -10.83759858, -11.23863248, -11.86106083, -11.67721755, -11.31617139, -11.82250451, -12.02998052, -12.17673882, -11.71174754, -11.64251468, -10.92995139, -11.81706614, -11.76353761, -11.82732821, -11.22686902, -11.67965339, -11.88312209, -12.160906, -11.93638016, -11.7118898, -11.52003449, -11.93423169, -12.18305969, -12.25241406, -11.77310599, -11.67450219, -11.79622349, -11.4361963, -11.72527214, -11.33853992, -11.48264379, -12.05167647, -12.06724359, -11.71870952, -11.63181215, -11.324431, -11.75162303, -11.65504851, -12.09371934, -12.56437478, -12.03806931, -11.06655731, -11.8033435, -12.04074259, -11.79700325, -11.84105862, -11.95818624, -11.35088737, -11.23968891, -11.51687067, -11.99391234, -11.77752182, -11.95002405, -10.80417135, -11.15113073, -11.53833992, -11.431271, -11.66668031, -11.81534306, -11.7854304, -11.98222214, -11.62302048, -11.54556635, -11.54322479, -12.07649255, -12.17084192, -10.81180002, -11.94261623, -11.82180156, -11.29729416, -12.1321693, -11.92107213, -12.52272166, -12.00161127, -11.90196786, -12.22308112, -11.56044077, -12.00110015, -10.91261422, -11.54602003, -11.30506841, -11.34929484, -11.31399853, -12.42810637, -12.1129144, -11.67262498, -12.52057014, -11.58062056, -11.45590925, -11.36629482, -12.18353657, -11.82870293, -11.39180448, -11.70817896, -10.85489866, -11.57559935, -11.4768686, -11.72087054, -11.38287161, -11.60925445, -11.29676694, -11.27164922, -11.32866973, -11.72793007, -11.37572987, -12.01687661, -11.91213739, -11.57685881, -12.1710852, -12.09721811, -11.86605861, -11.71930552, -11.16826434, -11.40522563, -11.72604418, -11.50919639, -11.62096399, -11.69609839, -11.68525865, -11.4975083 };
        private static readonly double[] _avgLungStDev = new double[] { 6.702249385,5.711017028,5.00917892,3.200956236,2.838256185,3.018241032,3.479270908,4.202338826,4.185894585,4.061426464,3.684184242,4.397604281,4.583224513,4.723214046,4.808880416,5.299931675,4.826616489,4.997054494,4.978466083,4.665695795,4.998815797,5.057937737,5.848700537,6.044620412,6.497295913,6.574159169,7.032142127,6.622239644,7.05497528,5.97501518,5.357533571,5.858317547,6.184486198,6.486147281,6.079587625,6.166761324,6.211881458,6.213426507,6.286884392,6.017521085,6.132960029,6.125064389,6.243004173,7.00809308,6.698143124,6.581588253,5.370711515,5.851790631,6.692815532,6.222490483,5.628814587,5.360696028,5.889256131,5.846376798,5.762353227,5.153878748,4.979736335,5.691767547,5.637485343,6.086384965,5.052108899,5.417866617,5.781246229,5.791294834,5.277014722,5.017782481,5.646667452,5.89969033,6.173566675,5.140854199,5.201001919,5.982471569,6.369447877,5.643721746,4.72308391,5.332885042,5.524477768,5.898298128,5.5293886,5.475296975,5.622119508,5.47977887,5.792473077,4.489994086,4.084940698,4.975745527,5.097062042,4.858686207,3.67875298,4.091644041,4.638258811,4.751085798,4.077791133,3.757557709,4.419795541,4.885850037,4.870067112,4.272095574,4.390589412,5.014568163,4.570469403,4.81534984,3.901718657,4.124783753,4.663284629,5.136729728,5.663003531,5.218470725,5.094803346,4.716538307,5.142304286,4.504507897,4.984455028,5.380228186,4.985427753,4.705884374,5.292235633,5.682329509,5.144083674,5.348287724,4.915252585,4.766543807,5.316335567,5.196846693,5.418252144,5.280436903,5.100487789,4.695088066,4.678121792,5.286485521,4.601735641,4.90038688,4.990143785,4.814454142,4.739212961,4.594399326,4.421850399,5.348968862,4.828802812,4.336111957,4.62332665,4.98733128,4.524888166,4.386592061,4.909015961,5.032902777,5.159835862,4.585283675,4.465383554,4.852214316,4.667206079,4.282298344,4.358665891,4.121609884,4.015277653,4.079324382,4.610471394,5.032040788,4.834896733,4.332873563,4.44260655,4.311449204,4.43233952,4.573285238,3.898657925,3.643603428,3.959076409,3.616093444,4.080421701,4.027367604,3.788691883,3.994310745,3.995640382,3.418685735,4.159042513,4.427560253,3.535115669,3.700949762,3.71650122,3.260754696,3.582979951,2.990524054,3.804311948,3.548623795,3.331204169,2.974985754,2.69265028,2.710501351,2.734141824,2.838930508,2.184567688,2.133988002,2.439488612,2.62719094,2.485643037,1.748607025,1.927160284,1.973067865,2.0160522,1.75322387,1.794278012,1.700049975,2.624673838,2.109337734,2.069838857,1.816789102,1.746368041,1.516662362,1.947027812,2.043918749,1.703225757,1.540211549,1.470570454,1.581883361,1.790258128,1.619030388,1.774017851,1.489120853,2.102139034,1.459727467,2.039143772,1.721406209,1.683817817,1.639688597,1.95692773,1.87650863,1.570651574,1.649833555,1.582415872,1.453469008,2.250303166,1.605367312,2.277363868,1.52793434,1.702985633,1.890858992,1.621231574,1.771369139,1.815162389,1.693866517,1.636664989,1.467365661,1.786966961,1.703464628,1.737780724,1.682036342,1.61391161,1.656446337,1.799905392,1.378128555,1.851674369,1.657925578,1.459763102,1.878121403,1.710193419,1.925536068,1.676101203,1.348967879,2.288313865,2.20222894,1.70804849,1.631889477,1.828995764,2.039989588,1.823363294,1.864387344,1.86326254,1.648698255,1.516454145,1.937094269,2.057565396,1.65834142,1.732677833,1.744619122,1.726806653,1.501695834,1.545370605,1.607721983,1.772520621,1.501475953,1.566875129,1.623068767,1.432195163,2.132208066,1.819678026,1.897002643,1.792516492,1.715667304,1.705605632,1.530442098,1.703638923,1.643173853,1.470685833,1.725432458,1.93748519,1.629918704,1.47077128,1.568268843,1.407649406,2.013074889,1.904206216,1.538045403,1.456696976,1.594851572,1.497993379,1.803565827,1.99739544,1.400485412,1.694416554,1.43975098,1.321483305,1.638741079,1.758065855,1.709278378,2.042061285,1.903055475,1.620173591,2.030928299,1.583230522,1.392837064,1.55386881,1.638427462,1.683189195,1.675230057,1.774207937,1.715362499,1.587362527,1.469197866,1.988374178,1.843456639,1.700496716,1.664235597,1.753485631,1.649145368,1.653147398,1.700655616,1.798719849,1.382837768,1.458202463,1.778280682,1.545474109,1.557103849,1.472611222,1.338051951,1.942143053,1.648564404,1.726874311,1.653438347,1.855586042,1.5133039,1.544837588,1.715037849,1.625267847,1.481916225,1.658142887,1.577023053,1.658410476,1.842330715,1.363335918,1.782779345,1.671765135,1.465478661,1.680975877,1.521879986,1.825236148,1.903365917,1.664719292,1.57589074,1.814438377,1.868210659,1.883296321,1.334382548,1.639904617,1.284089566,1.636691235,1.554428186,1.902959899,1.450674329,1.728523474,1.313807359,1.699785771,1.429866518,1.272164727,1.410623922,1.81203624,1.556402896,1.750896688,1.465365083,1.454990458,1.722280535,1.687544883,1.694024845,1.959945166,1.361384597,1.634603547,1.394558357,1.477188937,1.722724746,1.455691012,1.399528931,1.545008979,1.642365379,1.454031991,1.777512012,1.713469723,1.572529908,1.555556529,1.805421094,1.464323653,1.452930174,1.358321373,1.819293706,1.714009379,1.495230665,1.624818658,1.432415305,1.617912533,1.993760942,1.707303223,1.671713122,1.667739176,1.850955964,1.554530696,2.093365093,1.522683933,1.552073604,1.495191873,1.713465805,1.919230261,1.804529992,1.541285255,1.621960498,1.873379462,1.719712871,1.689199221,1.784968158,1.903051311,1.91683594,1.771462624,1.391323861,2.336650318,1.843742292,1.755828427,1.54615158,2.130564752,1.96357623,1.708811583,1.824675151,2.068198027,2.073828481,1.655062757,1.801245611,1.727036927,1.449939493,1.627230228,1.526606,1.55506055,1.646736316,1.897815491,1.965102544,1.676527944,1.806225238,2.012599374,1.670576074,1.851039894,1.60391143,1.895353556,1.697045037,1.598720852,1.627241864,1.720885555,1.736939213,1.421354264,1.43505443,1.816359009,1.81926255,1.325923058,1.585600588,1.526335652,1.549509273,1.661855328,1.531646819,1.533359524,1.408023067,1.702687993,1.652191332,1.891846181,1.899105884,1.606106216,1.484745242,1.634515479,1.99849668,1.961128675,1.569830652,1.795322965,1.359904056,1.678990964,1.496454425,1.542151976,1.756295959,1.689999424,1.514356471,1.735008933,1.470217986,1.604752627,1.918041988,1.695346038,1.337656586,1.642536217,1.519084677,1.484021157,1.314466569,1.530923022,1.795521387,1.631983828,1.51227828,1.839707814,1.282221509,1.468328844,1.537015721,1.408626358,1.495880472,1.418770614,1.627614501,1.697956638,1.624230709,1.996435128,1.847463578,1.765318257,1.474340154,1.682195368,1.518104314,1.425069246,1.73821457,1.409160663,1.628356159,1.583652936,1.587420895,1.557809078,1.321623243,1.633816363,1.430255289,1.793431443,1.456346448,1.611751659,1.384304568,1.717693514,1.838354941,2.027461034,2.065813291,1.693470849,1.65538605,1.870820109,1.399338843,1.551555469,1.677032077,1.741750302,1.57866298,1.820327917,1.502978917,2.099820601,1.644652613,1.911275711,1.63562405,1.54242079,1.373187419,1.676954879,1.814967717,2.019012126,1.738167819,1.749067751,1.764952277,1.834137121,1.376476844,1.30737596,1.468645024,1.49891645,1.558539593,1.707531467,1.757419812,1.77345873,1.528855737,1.41941301,1.409540292,1.482498966,1.350327375,1.35024135,1.363955123,1.760760793,1.518192623,1.629955135,1.514780069,1.46982761,1.527368191,1.413735704,2.028536102,1.73507049,1.454677112,1.63780667,1.485381168,1.748909778,1.531967188,1.37519212,1.721445331,1.80497587,2.280167367,1.806814417,1.581126149,1.582486906,2.052539829,1.502254096,1.549703452,1.788797754,1.757444129,1.983822536,1.521214992,1.609149245,1.860894992,1.800162208,1.713931789,1.890425535,1.670114412,1.645397977,1.854065821,1.76875625,1.790061643,1.481728177,1.732385763,1.919023105,1.697853222,1.524369454,2.037118238,1.276979095,1.644943383,1.415916762,1.733217728,1.776356252,1.623525205,1.531496499,1.530490013,1.708707347,1.912069488,1.863427524,1.479228061,1.514938452,1.553091062,1.462339426,1.932110924,1.809359866,1.623316579,1.733900254,1.766154946,1.677422186,1.529937585,1.672861987,1.715281379,2.043295046,1.548378977,1.471918736,1.456992394,1.550556761,1.978039303,1.865108098,1.544812887,1.891628148,1.509864824,1.393432427,1.521047899,1.635138112,1.793856958,1.505929779,1.706453183,1.832213206,1.754133978,1.576482784,1.634706899,1.488171582,2.09673073,1.678048476,1.58195676,1.584795854,1.422562017,1.46299509,1.487735963,1.4661994,1.703197227,1.329357691,1.345300269,1.43778574,1.515806495,1.527610652,1.777331783,1.701538705,1.572340752,1.609000735,1.562966645,1.636926619,1.834431879,1.373249498,1.563401666,1.662123928,1.587459107,2.013413417,1.754849118,1.908246096,1.694550327,1.447290118,1.621408449,1.667337325,1.308250781,1.410665537,1.791807606,1.635914395,1.512875414,1.538609897,1.270162545,1.347325147,1.462096235,1.953770409,1.739657391,1.463045474,1.917126428,1.416898127,1.712633553,1.661249705,2.066058292,1.530230038,1.481556263,1.707206109,1.839906766,1.530130362,1.538854805,1.538754352,1.632150538,1.506578003,1.366075784,1.611120643,1.686801316,1.647496064,1.760597466,1.387765968,1.471848956,1.59769392,1.711146907,1.621542198,1.641750863,1.279258405,1.604434496,1.512915812,1.741603822,1.713954668,1.800203611,1.647213081,1.464609754,1.491845137,2.22042246,1.739319435,1.517441339,1.396535169,1.493617909,1.598207502,1.74368633,1.393640019,1.519010272,1.740859359,1.555480464,1.694513616,1.407720183,1.573213243,1.618408041,1.588368488,1.524031864,1.64028098,1.728175882,1.829078477,1.524601912,1.530803139,1.640788004,1.597513402,1.419079594,1.707597224,1.72775117,1.721338444,1.846353553,1.464871388,1.558089811,2.201358563,1.580062665,1.55542179,1.825659001,1.64798307,1.692864059,1.837640066,1.758429602,2.00885746,1.407552115,1.478343065,1.475731556,1.249309444,1.390022798,1.486491956,1.666311978,1.603691572,1.490589428,1.807927899,2.036083266,1.568069028,1.607372462,1.688139826,1.37720632,1.556757817,1.711387015,1.709567473,1.707231155,1.519850904,1.5408444,1.682558136,1.777681509,1.982195858,1.434746545,1.772324446,1.629151788,1.587051958,1.609212837,1.823306595,1.642359423,1.955745618,1.664918802,1.596277884,1.610355544,1.6777794,1.879449345,1.816208252,2.118638317,1.302063664,1.494493354,1.68314899,1.491631996,1.379281678,1.52702968,1.29812285,1.480448072,1.505165066,1.857319985,1.652687633,1.284595634,1.618053517,1.510022579,1.968772825,2.342666477,1.330387898,1.361015574,1.636079042,1.672507604,1.818159775,1.431596241,1.793236472,1.652769118,1.685115966,1.538566747,1.708096105,1.700290026,1.95558507,1.588702342,1.437225563,1.605476723,1.670128521,1.650140375,1.663120296,1.312539562,1.645530164,1.601085028,2.131596951,1.898519306,1.525642721,1.625505759,1.671406247,1.67502551,1.57077069,1.582089772,1.941714686,1.642970699,1.619377908,1.760516453,1.971313651,2.149584155,1.39455266,1.492730687,1.78100837,1.728394762,1.833576471,1.760375371,1.581900703,1.762151707,1.61097243,1.482645951,1.895273672,1.498817352,1.619031631,1.42075522,1.817832927,1.473446685,1.830351728,1.641160294,1.659139039,1.439984912,1.772656923,1.746285152,1.964517338,1.512389738,1.459233273,2.068643012,2.144057845,1.667466138,1.556940362,1.527704955,1.742861382,1.452101633,1.47876305,1.515697425,1.311105726,1.576677218,1.591630171,1.747769348,1.652486098,1.731704915,1.881478659,1.779910813,1.834231674,1.590888234,1.326563099,1.914679633,1.453938686,1.599580973,1.587900032,1.550655157,1.653208731,1.690043427,1.583344378,1.675793441,1.497731774,2.126608087,1.734557104,1.59592175,1.326488042,1.469366862,1.809581747,1.436274228,1.71259698,1.277243532,1.533403223,1.821986311,1.59110959,1.566322794,1.656292752,1.743328418,1.955161118,1.934691882,1.411325698,2.192886836,1.793140478,1.550321345,1.706047924,2.030815505,2.02161258,1.631030601,1.781721088,1.44712869,1.830978138,1.388210685,1.720676934,1.533218812,1.771958244,1.878101256,1.442755877,2.025765314,1.347296466,1.375381761,1.509023305,1.477787211,1.897274609,1.898402027,1.582249995,1.701066136,1.742532304,1.584666726,1.468052142,1.684551731,1.745236231,1.714118967,2.429738443,1.45878564,1.538785818,1.78812682,1.717963754,1.636609338,1.606694807,1.955135202,1.521810425,1.418972922,1.46189501,1.982605853,1.493223271,1.287738617,1.529530922,1.606697168,1.674110648,1.664250479,1.719544609,1.308772998,1.824653953,1.439671436,1.416957593,1.784844047,1.565550879,1.460418731,1.952683564,1.408835963,1.832851911,1.548922471,1.526039926,1.718247082,1.714134126,1.834848942,1.791669232,1.723120967,1.821360384,1.40182085,1.440221921,1.95086711,1.332225467,1.474654312,1.349434076,1.487836318,1.652925184,1.794047003,1.442572017,2.177765027,1.654155772,1.677634435,1.653155497,1.580940671,1.451405464,1.884373097,1.974689986,1.517688697,1.587899428,1.363870792,1.745927759,1.653466628,1.491990495,1.34414695,1.526924372,1.709641722,1.65460047,1.666589196,1.42230074,1.525131461,1.920202292,1.589944192,1.887418711,1.630185928,1.314736327,1.503873847,1.574972974,1.423047051,1.64817321,1.937512644,1.647344174,1.719497456,1.425172637,1.783077411,1.505771677,1.506710237,1.676180535,1.938614618,1.447789233,1.482857129,1.271654527,1.670261892,1.766182643,2.144740409,1.719801615,1.779159032,1.775038934,1.527583109,1.378953269,1.540823948,1.558170025,1.321482035,1.536452573,1.384242471,1.640944224,1.644624174,1.537439183,1.775489345,1.977339497,1.815025475,1.467039788,1.544988562,1.750081084,1.692063014,1.727174857,1.672706101,1.551044082,2.021576763,1.783963714,1.844961383,1.606489229,1.445125676,1.218923358,1.715001067,1.899362319,1.653646356,1.790702485,1.606206794,1.449632792,1.541149715,1.660353119,1.599062864,1.512213536,1.928904554,1.623930705,1.149968621,1.480251275,1.773713966,1.61915746,1.713706076,1.700114298,1.593920795,1.980861402,1.388199765,1.367051078,1.53800218,1.676262723,1.876710208,1.736352265,1.622497371,1.829439527,1.834296966,1.286786728,1.504686005,1.656145975,1.702026119,1.620527098,1.654967407,1.28096413,1.373619168,1.461190266,1.688677441,1.47307641,1.94847106,1.494909269,1.317736882,1.558451454,1.621561124,1.676996406,1.51020465,1.697882915,1.45834588,1.44327928,1.71490567,1.712705278,1.666059241,1.420816707,2.093597788,1.366173843,1.751286913,1.669954276,1.426338492,1.59910543,1.590799095,1.406530601,1.608601453,1.672320384,1.881283075,1.273217869,1.584939277,1.467995583,1.363221942,1.692790861,1.614167697,1.700156,1.633465399,1.937177364,1.359973593,1.628124773,1.911193123,1.775416193,1.791190907,1.806218519,1.689550116,1.95406465,1.669176808,1.404901392,1.753141183,1.409944364,1.554041621,1.728216677,1.450516517,1.364234033,1.844092348,1.782267021,1.549209871,1.669436149,1.673436467,1.530761503,1.6702604,1.668286276,1.38521019,1.790178493,1.742175054,1.375461906,1.548302678,1.531030946,1.43247613,1.763583908,1.800293785,1.552632039,1.600689799,1.524796257,1.906567461,1.581717678,1.661810104,1.511289294,1.587856763,1.997902858,2.068435412,1.526235289,1.593989932,1.80471451,1.756098967,1.669529996,1.823606343,1.910609776,1.629966521,1.560635317,1.424096479,1.91128655,1.722141313,1.548623511,1.485703223,1.442123032,1.238412382,1.711713359,1.419366573,1.356212088,1.636551475,1.462298249,2.025344798,1.590291657,1.465714174,1.709765272,1.726086322,1.536422786,2.115161743,1.393359676,1.453702954,1.354350543,1.375076727,1.51853443,1.505971973,1.407091904,1.532963435,1.553367924,1.601456758,1.376670971,1.815901358,1.579209359,1.774933057,1.674481502,1.814098828,1.751487714,1.922176855,1.58391009,1.73446784,1.77517604,1.939785667,1.60675071,1.388191684,1.518666905,1.750507072,1.89670564,1.633830939,1.902510282,1.602183415,2.02634703,1.833119387,1.523327586,1.49930168,1.654566452,1.502472811,1.503325102,1.232350175,1.315339904,1.66737057,1.46829978,1.234590647,1.562375227,1.575786399,1.693194595,1.480380743,1.461257591,1.741458804,1.699200342,1.607787263,1.750416804,1.556741885,1.437242003,1.518740427,1.765729918,1.923715187,1.94523174,1.859721744,1.575782773,1.603043987,1.878422273,1.697005221,1.687583638,1.695442854,1.728649156,1.375155449,1.213088582,1.586596264,1.591588997,1.577740049,1.3723845,1.596476448,1.676586941,1.823591421,1.326097418,1.606900142,1.393318725,2.077604648,1.611686293,1.510993308,1.879901601,1.732266481,1.616041752,1.749706522,1.446327173,1.451202016,1.493288268,1.477059385,1.394633064,1.730348076,1.657985003,1.706416033,1.526185692,1.837430953,2.137903821,1.755995142,1.669456579,1.469054261,1.542590468,1.776369883,2.242632263,1.572596387,1.461192832,1.621329392,1.617772973,1.779646231,1.506157636,1.273993454,1.274838486,1.487770764,1.471669916,1.47920866,1.515236234,1.871942244,1.842927412,1.896287292,1.727819589,1.541688891,1.636690815,1.747534171,1.372702918,1.325724866,1.434758865,1.75997676,1.331761905,1.349095453,1.728619454,1.538348604,1.415597087,1.663842847,1.656536876,1.578493696,1.620713808,1.522909496,1.914664147,1.857870248,1.508142726,1.542181227,1.641279058,1.719258449,1.426144025,1.436413268,1.506965571,2.205486691,1.8260158,1.462865113,1.660615288,1.726789533,1.778505508,1.604795386,1.710645675,1.890629152,1.501112279,1.393068782,1.472361708,1.563716619,1.692788925,1.446068672,1.493935404,1.41354451,1.458122544,1.469565988,1.54936465,1.728251907,1.885722435,1.614069907,1.638998229,1.770877988,1.71224067,1.638856428,1.64021061,1.561441904,1.483762286,1.571921684,1.581684549,1.533932214,1.823340426,2.203390772,1.686667932,1.711347251,1.986122338,1.705528998,1.79566429,1.424982883,1.657362944,1.74781349,1.379031102,1.574271334,1.579097744,1.633628291,1.582393536,1.355952461,1.783945759,1.571537775,1.313255671,1.640722422,1.414335103,1.616600805,1.779467438,1.779083333,1.382559328,1.433189779,1.974469118,1.940146159,1.866973017,1.653192684,1.50921909,1.959502794,1.821734594,1.577889621,1.547152773,1.640617793,1.651058661,1.531898037,1.518205371,1.740803865,1.898804833,1.588369319,1.488149882,1.510758752,1.548092451,1.831868412,1.57538133,1.537072354,1.592415615,1.628482242,1.753319625,2.069032567,1.635565803,1.72368164,1.538596188,1.330967334,1.379681246,1.473830553,1.771372031,1.556090995,1.52847899,1.594039224,1.903727532,1.4863955,1.635425063,1.77983185,1.847783721,1.530572185,1.513903543,2.006502541,1.690148506,1.863559993,1.610815498,1.196734011,1.39464583,1.436141215,1.43961371,1.577690706,1.651101093,1.734263111,1.542707354,1.641038444,1.367820105,1.393911558,1.39992942,1.589569974,1.478781902,1.550029304,1.402997657,1.562889119,1.346600067,1.573868069,1.686170005,1.931706314,1.541168003,1.736645213,1.664727801,1.444746629,1.674136398,1.643429465,1.79853224,1.570345643,1.413338164,1.677282911,1.838408522,1.709274383,1.723593806,1.704523826,1.627635137,1.40627855,1.764172672,1.784915346,2.002582042,1.791915505,1.612939693,1.628341814,1.654644237,1.550908499,1.979868211,1.586216759,1.796994341,1.77133206,1.615744761,1.537838651,1.588726673,1.642209765,1.691126678,1.683083534,1.759326855,1.657418364,1.54371742,1.557283752,1.563821223,1.530849497,1.475314294,1.64708152,1.778989365,1.732913585,1.354032807,1.805331162,1.526484862,1.698101822,1.667828422,1.508930553,1.535961821,1.872510581,1.945926276,1.410919587,1.796842281,1.703307565,1.449435119,1.663430359,1.566816111,1.639558312,1.765889644,1.805146451,1.722360485,1.623370576,1.798642799,1.573895342,1.756288739,1.47194013,1.506429051,1.515844181,1.252032516,1.77696892,2.003670182,1.972097071,1.548289447,1.633947069,1.852574109,1.662775563,1.808080962,1.589627859,1.869763841,1.44694774,1.603934569,1.816512101,1.578793044,1.336085387,1.620294536,1.47740587,1.632670437,1.584071738,1.40763363,1.817731584,1.191213846,1.779327481,1.558090857,1.816948218,1.473304039,1.38083311,1.942485549,1.698005177,1.799620194,1.402662897,1.606844903,1.496982659,1.620609617,1.672770372,1.725457187,1.438609185,1.481705496,1.661221812,1.781518152,1.511034334,1.807307807,1.844652371,1.788227517,1.441190251,1.638201388,1.434599392,1.567354667,1.729998427,1.748261161,1.652521525,1.648066229,1.703004465,1.4549057,1.55591848,1.577855738,1.552141303,1.756137207,1.655833233,1.857165503,1.86950919,1.601625474,1.586027143,1.410188872,1.853490364,1.718339239,1.705842474,1.541043216,1.558363477,1.695851407,1.484688961,1.508759477,1.510224115,1.503659562,1.397150651,1.503939451,1.603226185,1.649385109,1.73968995,1.671057137,1.629385925,1.605402813,1.362649728,1.386514502,1.533309659,1.68331356,1.674504519,1.567598746,1.769833124,1.490052612,1.811361971,1.798544979,1.96344313,1.770481526,1.56467229,1.822618449,1.687117625,1.599516335,1.687787383,1.48521538,1.539911914,1.569143813,1.584966735,1.657398476,1.674402894,1.389594454,1.729819832,1.458338698,1.577542802,1.722829171,1.55151508,1.27470552,1.674620396,1.929034224,1.681430765,1.363981727,1.770542787,1.540840772,1.639625152,1.914563509,1.40423226,1.657329714,1.661338427,1.867380896,1.57119284,1.708851055,1.682476482,1.460659217,1.356311111,1.670483912,1.876222161,1.851055895,1.666901136,1.609645803,1.431376171,1.508390149,1.613946441,1.583861857,2.079737285,1.596101444,1.826760504,1.851182636,1.531165337,1.488964032,1.607780874,1.634983689,1.631016201,1.636429613,1.493019241,1.641491485,1.347406281,1.600151884,1.500797611,2.282215411,1.825017875,1.728826847,1.462789892,1.516699658,1.673623227,1.540298046,1.579101619,1.742653338,1.78508571,1.658307016,1.658282964,1.40886817,1.480899362,1.535408753,2.02702911,1.818755197,1.520388962,1.465081511,1.469337736,1.886245629,1.720249028,1.671239122,1.500490959,1.681565193,2.129177201,1.440073498,1.795462826,1.685317717,1.574536955,1.413746189,1.760536504,1.598779857,1.72425756,1.627544054,1.551693258,1.5738114,1.441089451,1.410896192,1.702969297,1.598687495,1.490826922,1.506846223,1.555467925,1.661716789,2.054588326,1.379870324,1.581021612,1.395675824,1.499041661,1.572035931,1.475952209,1.371044061,2.073402795,1.768096086,1.508484035,1.398833249,1.727634907,1.471083822,1.592325473,1.603678972,1.858595514,1.515592678,1.387084817,1.764339647,1.37942633,1.401210474,1.38205144,1.608337318,1.825255124,1.697014089,1.619060369,1.490316714,1.717371237,2.002026726,1.53460304,1.577907668,1.529315898,1.526643015,1.846943885,1.740580334,1.76022612,1.375141868,1.442005765,1.945036564,1.522549283,1.774473107,1.460894085,1.393891387,1.804293699,1.753701324,1.499444305,1.802600502,2.180832398,1.387799297,1.696503102,1.822038691,1.862567078,1.621254735,1.553051792,1.566672645,1.640158893,1.686235739,1.380065153,1.610006354,1.700717056,1.577381904,1.489938237,1.235935243,1.336715365,1.598319561,1.597483488,2.083734931,1.663059249,1.786275822,1.720656986,1.156472802,1.946235988,1.421607153,1.485284495,1.527211265,1.624480675,1.707460078,1.398904969,1.557934969,1.649221482,1.567553848,1.825327607,1.366225031,1.410412967,1.324869152,1.425612643,1.809859187,1.5388463,1.394182176,1.462043857,1.85298224,1.426169026,1.986778248,1.714017119,1.6601297,1.600028218,2.012185584,1.562521573,1.543371144,1.533070238,1.317861326,1.313893786,1.40073914,1.30144426,1.667905253,1.557745524,1.542337275,1.708908772,1.754075519,1.938690239,1.694493883,1.3677607,1.628191903,1.662183888,1.525254938,2.044867411,1.670858878,1.305688874,1.472074297,1.691637098,1.907051134,1.665587844,1.871709363,1.495481315,1.810070844,1.599173344,1.727331923,1.719949472,1.783347001,2.028689847,1.715543268,1.6887436,1.876455053,1.391849668,1.559032432,1.87884838,1.518540093,1.665752485,1.339515113,1.494585164,1.383679724,1.551976783,1.551589792,1.468702048,1.753389041,1.827247958,1.292736506,2.028159104,1.668286745,1.856622672,1.449701647,2.031298561,1.658306072,1.74456015,1.76170097,1.684960234,1.508920584,1.925275756,1.671155842,1.45439428,1.597237261,1.604435822,1.561128081,1.551332406,1.514510897,1.64359566,1.465824918,1.555070624,1.482615025,1.889635156,1.342409859,1.652992238,1.390992638,1.432845809,1.42655854,2.029756068,2.048223664,1.721237983,1.454778902,1.612144595,1.768384516,1.477321545,1.374919023,1.999033809,1.707826044,1.5710112,1.615453865,1.707324038,1.672505405,1.459461961,1.480119459,1.850855952,1.490325116,1.79284324,1.756587476,1.356475176,1.262227481,1.387575754,1.505206507,1.571733444,1.991853269,1.804570719,1.787192528,1.562656816,1.509198194,1.906872183,1.528256955,1.694176736,1.522412524,1.429693126,1.489767354,1.645645096,1.540417722,1.428962746,1.886532917,1.546021857,1.622949282,1.632642732,1.493652085,1.516772085,1.509210953,1.55493803,1.482263076,1.471368912,1.544910889,1.582654245,1.571366285,1.629628567,1.953066493,1.547756221,1.4143612,1.300832801,1.456458832,1.582051001,1.554976916,1.544405594,1.370735271,1.692954154,1.425324093,1.486199725,1.563801187,1.45784447,1.610552724,1.501005453,1.446604113,1.602188868,1.604121457,1.863885646,1.686635064,1.693986143,1.49962775,1.281470691 };

        private void ButtonNAvgFFT_Click(object sender, RoutedEventArgs e)
        {
            Dictionary<string, double[]> avgFFTs = new Dictionary<string, double[]>(Spectrogram.SelectedXPoints.Count);
            Dictionary<string, double[]> avgFFTsDenoise = new Dictionary<string, double[]>(Spectrogram.SelectedXPoints.Count);
            Dictionary<string, double[]> snrs = new Dictionary<string, double[]>(Spectrogram.SelectedXPoints.Count);
            Dictionary<string, double[]> compSnrs = new Dictionary<string, double[]>(Spectrogram.SelectedXPoints.Count);

            // Performs analysis to find average frequency data
            if (Spectrogram.SelectedXPoints.Count > 1)
            {
                if (MessageBox.Show("Display all on same graph?", "Average FTT", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    // Get highest average amp.
                    double maxAvgAmp = 0;
                    for (int i = 1; i < Spectrogram.SelectedXPoints.Count; i++)
                    {
                        SpectrogramPane.XSelection selection = Spectrogram.SelectedXPoints[i];

                        double currentAvgAmp = CalcAvgAmplitude(selection.AverageFFT, 100, 2000, false, true);

                        if (currentAvgAmp > maxAvgAmp)
                        {
                            maxAvgAmp = currentAvgAmp;
                        }
                    }

                    double[] normAvg = Normalise(_avgLung);
                    for (int i = 0; i < Spectrogram.SelectedXPoints.Count; i++)
                    {
                        SpectrogramPane.XSelection selection = Spectrogram.SelectedXPoints[i];

                        if (selection.NoiseSelection == null)
                        {
                            selection.NoiseSelection = Spectrogram.SelectedXPoints[0];
                        }

                        double[] normSnr = Normalise(selection.SNR);

                        snrs.Add(selection.ToString(), selection.SNR);
                        snrs.Add(selection.ToString() + ".Norm", selection.NormSNR);

                        double[] compSnr = new double[selection.SNR.Length];

                        for (int j = 0; j < compSnr.Length; j++)
                        {
                            compSnr[j] = normAvg[j] - normSnr[j];
                        }
                        compSnrs.Add(selection.ToString(), compSnr);

                        avgFFTs.Add(selection.ToString(), selection.AverageFFT);

                        //avgFFTsDenoise.Add(selection.ToString(), RemoveNoise(avgFFT, noiseFFT, maxAvgAmp));
                    }

                    compSnrs.Add("StDev+", _avgLungStDev);
                    compSnrs.Add("StDev-", ArrayMult(_avgLungStDev, -1));

                    snrs.Add("Norm. Avg.", normAvg);

                    new GraphWindow(avgFFTs, "Average FFT", "Frequency (Hz)", "Amplitude (dB)", (1.00 / FftWindowSize)).Show();
                    new GraphWindow(compSnrs, "SNR vs Avg SNR", "Frequency (Hz)", "SNR (dB)", (1.00 / FftWindowSize)).Show();
                    //new GraphWindow(avgFFTsDenoise, "Average FFT (Noise Removed)", "Frequency (Hz)", "Amplitude (dB)", (1.00 / FftWindowSize)).Show();
                    new GraphWindow(snrs, "Signal to Noise Ratio", "Frequency (Hz)", "SNR (dB)", (1.00 / FftWindowSize)).Show();
                }
                else
                {
                    foreach (pHMb.Spectrogram.SpectrogramPane.XSelection selection in Spectrogram.SelectedXPoints)
                    {
                        new GraphWindow(new Dictionary<string, List<double[]>>() { { selection.ToString(), selection.GetWindows() } }, "Average FFT", "Frequency (Hz)", "Amplitude (dB)", (1.00 / FftWindowSize)).Show();
                    }

                }
            }
        }

        private double[] ArrayMult(double[] array, double mult)
        {
            double[] newArray = new double[array.Length];

            for (int i = 0; i < array.Length; i++)
            {
                newArray[i] = array[i] * mult;
            }

            return newArray;
        }

        private double[] Normalise(double[] snr)
        {
            double avgDb100_2000 = CalcAvgAmplitude(snr, 100, 2000, false, false);
            //double avgDb100_2000_Avg = CalcAvgAmplitude(_avgLung, 100, 2000, false, false);

            double normFactor = avgDb100_2000 - 0;

            double[] normSnr = new double[snr.Length];
            for (int i = 0; i < snr.Length; i++)
            {
                normSnr[i] = snr[i] - normFactor;
            }

            return normSnr;
        }

        private double[] RemoveNoise(double[] fftAvg, double[] noiseAvg, double maxAvgAmp)
        {
            // Normalise
            // First get avg of 'noisy' area
            double[] absFftAvg = new double[fftAvg.Length];
            double[] absNoiseAvg = new double[noiseAvg.Length];
            double[] absDenoiseAvg = new double[noiseAvg.Length];
            for (int i = 0; i < absFftAvg.Length; i++)
            {
                absFftAvg[i] = Math.Pow(10, fftAvg[i] / 20);
                absNoiseAvg[i] = Math.Pow(10, noiseAvg[i] / 20);

                //absDenoiseAvg[i] = absFftAvg[i] - absNoiseAvg[i];
                absDenoiseAvg[i] = absFftAvg[i];
            }

            double avgNoiseDb = CalcAvgAmplitude(absDenoiseAvg, 2000, 20000, true, true);

            //double adjustmentFactor = avgNoiseDb / Math.Pow(10, -130 / 20);
            double adjustmentFactor = avgNoiseDb / maxAvgAmp;

            // Now alter wave
            for (int fftId = 0; fftId < absFftAvg.Length; fftId++)
            {
                //absDenoiseAvg[fftId] = absDenoiseAvg[fftId] / adjustmentFactor;
            }


            double[] result = new double[fftAvg.Length];
            for (int i = 0; i < fftAvg.Length; i++)
            {
                //result[i] = absDenoiseAvg[i];
                result[i] = 20 * Math.Log10(absDenoiseAvg[i] - absNoiseAvg[i]);
                //result[i] = Math.Pow(10, fftAvg[i]/20);

                //result[i] = 20 * Math.Log10(absFftAvg[i]);
            }
            
            return result;
        }

        private double CalcAvgAmplitude(double[] avgFft, int startF, int endF, bool absIn = false, bool absOut = false)
        {
            double avgAmp = 0;
            int x = 0;
            for (x = (int)(startF / (1.00 / FftWindowSize)); x < (int)(endF / (1.00 / FftWindowSize)); x++)
            //for (x = 0; x < avgFft.Length; x++)
            {
                if (absIn)
                {
                    avgAmp += avgFft[x];
                }
                else
                {
                    avgAmp += Math.Pow(10, avgFft[x] / 20.00);
                }
            }

            avgAmp /= x;

            if (absOut)
            {
                return avgAmp;
            }
            else
            {
                return 20 * Math.Log10(avgAmp);
            }
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            using (BinaryWriter selectionsWriter = new BinaryWriter(File.Open(WavFileName.ToLower().Replace(".wav", ".spsel"), FileMode.Create)))
            {
                selectionsWriter.Write(new char[] { 'P', 'H', 'S', 'P', 
                                                    'E', 'C', 'T', '\0', 
                                                    'V', '1', '0', '2' });

                selectionsWriter.Write(Spectrogram.SelectedXPoints.Count);

                WriteSelections(Spectrogram.SelectedXPoints, selectionsWriter);

                MessageBox.Show(this, "Selections saved.", "Save Selections", MessageBoxButton.OK);
            }
        }

        public static void WriteSelections(ObservableCollection<SpectrogramPane.XSelection> selections, BinaryWriter selectionsWriter)
        {
            SpectrogramPane.XSelection currentSelection;
            for (int i = 0; i < selections.Count; i++)
            {
                currentSelection = selections[i];

                WriteSelection(currentSelection, selectionsWriter, selections);
            }
        }

        public static void WriteSelection(SpectrogramPane.XSelection selection, BinaryWriter selectionsWriter, ObservableCollection<SpectrogramPane.XSelection> selections)
        {
            selectionsWriter.Write(selection.Name);

            selectionsWriter.Write(selection.StartX);
            selectionsWriter.Write(selection.EndX);
            selectionsWriter.Write(selection.StartLineColor.ToArgb());
            selectionsWriter.Write(selection.EndLineColor.ToArgb());

            if (selection.NoiseSelection != null && selection.NoiseSelection != selection)
            {
                selectionsWriter.Write(selections.IndexOf(selection.NoiseSelection));
            }
            else
            {
                selectionsWriter.Write(-1);
            }

            if (selection.GetType() == typeof(SpectrogramPane.XSelectionGroup))
            {
                SpectrogramPane.XSelectionGroup currentSelGrp = (SpectrogramPane.XSelectionGroup)selection;

                // Number of children
                selectionsWriter.Write(currentSelGrp.ChildSelections.Count);
                foreach (SpectrogramPane.XSelection childSel in currentSelGrp.ChildSelections)
                {
                    WriteSelection(childSel, selectionsWriter, selections);
                }
            }
            else
            {
                selectionsWriter.Write(0); // No children
            }
        }

        public static SpectrogramPane.XSelection LoadSelection(BinaryReader selectionsReader, SpectrogramPane spect, bool includesNoise = true, 
            Dictionary<SpectrogramPane.XSelection, int> noiseSelections = null, int fileOffset = 0, int noiseIndexOffset = 0)
        {
            string name = selectionsReader.ReadString();
            int startX = selectionsReader.ReadInt32() + fileOffset;
            int endX = selectionsReader.ReadInt32() + fileOffset;

            byte[] startColor = selectionsReader.ReadBytes(4);
            byte[] endColor = selectionsReader.ReadBytes(4);

            int noiseIndex = -1;
            if (includesNoise && noiseSelections != null)
            {
                noiseIndex = selectionsReader.ReadInt32() + noiseIndexOffset;
            }

            int childCount = selectionsReader.ReadInt32();

            SpectrogramPane.XSelection selection;

            if (childCount > 0)
            {
                selection = new SpectrogramPane.XSelectionGroup(spect);

                for (int i = 0; i < childCount; i++)
                {
                    ((SpectrogramPane.XSelectionGroup)selection).ChildSelections.Add(LoadSelection(selectionsReader, spect, 
                        includesNoise, noiseSelections, fileOffset, noiseIndexOffset));
                }
            }
            else
            {
                selection = new SpectrogramPane.XSelection(spect);
            }

            selection.Name = name;
            selection.StartX = startX;
            selection.EndX = endX;

            selection.StartLineColor = System.Drawing.Color.FromArgb(startColor[3], startColor[2], startColor[1], startColor[0]);
            selection.EndLineColor = System.Drawing.Color.FromArgb(endColor[3], endColor[2], endColor[1], endColor[0]);

            if (noiseSelections != null)
            {
                noiseSelections.Add(selection, noiseIndex);
            }

            return selection;
        }

        public void LoadSelections(int fileOffset = 0)
        {
            if (File.Exists(WavFileName.ToLower().Replace(".wav", ".spsel")))
            {
                using (BinaryReader selectionsReader = new BinaryReader(File.Open(WavFileName.ToLower().Replace(".wav", ".spsel"), FileMode.Open)))
                {
                    if (new string(selectionsReader.ReadChars(7)) == "PHSPECT")
                    {
                        selectionsReader.ReadChar();
                        string verString = new string(selectionsReader.ReadChars(4));

                        int numSelections;
                        switch (verString)
                        {
                            case "V100":
                                numSelections = selectionsReader.ReadInt32();

                                for (int i = 0; i < numSelections; i++)
                                {
                                    SpectrogramPane.XSelection selection = new SpectrogramPane.XSelection(Spectrogram);

                                    selection.Name = selectionsReader.ReadString();
                                    selection.StartX = selectionsReader.ReadInt32() + fileOffset;
                                    selection.EndX = selectionsReader.ReadInt32() + fileOffset;

                                    byte[] startColor = selectionsReader.ReadBytes(4);
                                    byte[] endColor = selectionsReader.ReadBytes(4);

                                    selection.StartLineColor = System.Drawing.Color.FromArgb(startColor[3], startColor[2], startColor[1], startColor[0]);
                                    selection.EndLineColor = System.Drawing.Color.FromArgb(endColor[3], endColor[2], endColor[1], endColor[0]);

                                    Spectrogram.SelectedXPoints.Add(selection);
                                }
                                break;

                            case "V101":
                                numSelections = selectionsReader.ReadInt32();

                                for (int i = 0; i < numSelections; i++)
                                {
                                    Spectrogram.SelectedXPoints.Add(LoadSelection(selectionsReader, Spectrogram, false));
                                }
                                break;

                            case "V102":
                                numSelections = selectionsReader.ReadInt32();

                                Dictionary<SpectrogramPane.XSelection, int> noiseSelections = new Dictionary<SpectrogramPane.XSelection,int>();
                                for (int i = 0; i < numSelections; i++)
                                {
                                    Spectrogram.SelectedXPoints.Add(LoadSelection(selectionsReader, Spectrogram, true, noiseSelections, fileOffset));
                                }

                                loadNoiseSels(Spectrogram.SelectedXPoints, noiseSelections, Spectrogram.SelectedXPoints);
                                break;

                            default:
                                MessageBox.Show("Selections file not compatible with this version.\nSelections not loaded.", "Incompatible selections", MessageBoxButton.OK, MessageBoxImage.Error);
                                break;
                        }

                        
                    }
                    else
                    {
                        Console.WriteLine("Invalid selections file");
                    }
                }
            }


            listBoxSelections.ItemsSource = Spectrogram.SelectedXPoints;
        }

        public static void loadNoiseSels(IEnumerable<SpectrogramPane.XSelection> selections, 
            Dictionary<SpectrogramPane.XSelection, int> noiseSelections, ObservableCollection<SpectrogramPane.XSelection> selectionsTarget)
        {
            foreach (SpectrogramPane.XSelection selection in selections)
            {
                if (selection.GetType() == typeof(SpectrogramPane.XSelectionGroup))
                {
                    loadNoiseSels(((SpectrogramPane.XSelectionGroup)selection).ChildSelections, noiseSelections, selectionsTarget);
                }

                if (noiseSelections.ContainsKey(selection) && noiseSelections[selection] >= 0)
                {
                    selection.NoiseSelection = selectionsTarget[noiseSelections[selection]];
                }
            }
        }

        private void menuItemRename_Click(object sender, RoutedEventArgs e)
        {
            if (this.listBoxSelections.SelectedIndex != -1)
            {
                SpectrogramPane.XSelection selection = (SpectrogramPane.XSelection)listBoxSelections.SelectedItem;

                RenameWindow renameWin = new RenameWindow();

                renameWin.Name = selection.Name;

                if (renameWin.ShowDialog() == true)
                {
                    selection.Name = renameWin.Name;
                }

                listBoxSelections.ItemsSource = null;
                listBoxSelections.ItemsSource = Spectrogram.SelectedXPoints;
            }
        }

        private void menuItemDelete_Click(object sender, RoutedEventArgs e)
        {
            DeleteSelected();
        }

        private void ButtonAutoName_Click(object sender, RoutedEventArgs e)
        {
            RenameWindow renameDialog = new RenameWindow(true);

            if (renameDialog.ShowDialog() == true)
            {
                for (int i = 0; i < Spectrogram.SelectedXPoints.Count; i++)
                {
                    SpectrogramPane.XSelection selection = Spectrogram.SelectedXPoints[i];

                    if (i == 0)
                    {
                        selection.Name = renameDialog.Name + ":Noise";
                    }
                    else if ((i - 1) % 2 == 0)
                    {
                        if (renameDialog.StartsWithIn)
                        {
                            selection.Name = string.Format("{0}:In.{1}", renameDialog.Name, (i - 1) / 2 + 1);
                        }
                        else
                        {
                            selection.Name = string.Format("{0}:Out.{1}", renameDialog.Name, (i - 1) / 2 + 1);
                        }
                    }
                    else
                    {
                        if (renameDialog.StartsWithIn)
                        {
                            selection.Name = string.Format("{0}:Out.{1}", renameDialog.Name, (i - 1) / 2 + 1);
                        }
                        else
                        {
                            selection.Name = string.Format("{0}:In.{1}", renameDialog.Name, (i - 1) / 2 + 1);
                        }
                    }
                }
                listBoxSelections.ItemsSource = null;
                listBoxSelections.ItemsSource = Spectrogram.SelectedXPoints;
            }
        }

        private void listBoxSelections_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedSelections = listBoxSelections.SelectedItems;
            foreach (SpectrogramPane.XSelection selection in Spectrogram.SelectedXPoints)
            {
                selection.IsHighlighted = selectedSelections.Contains(selection);
            }

            Spectrogram.UpdateOverlay();
        }

        private void ButtonGroup_Click(object sender, RoutedEventArgs e)
        {
            var selectedSelections = listBoxSelections.SelectedItems;

            if (selectedSelections.Count > 1)
            {
                RenameWindow renameWin = new RenameWindow();

                renameWin.Name = "New Group";

                if (renameWin.ShowDialog() == true)
                {
                    SpectrogramPane.XSelectionGroup newSelGroup = new SpectrogramPane.XSelectionGroup(Spectrogram);

                    foreach (SpectrogramPane.XSelection sel in selectedSelections)
                    {
                        newSelGroup.ChildSelections.Add(sel);
                    }

                    newSelGroup.Name = renameWin.Name;

                    Spectrogram.SelectedXPoints.Add(newSelGroup);
                }

                listBoxSelections.ItemsSource = null;
                listBoxSelections.ItemsSource = Spectrogram.SelectedXPoints;
            }
        }

        private void menuItemSetNoise_Click(object sender, RoutedEventArgs e)
        {
            foreach (SpectrogramPane.XSelection currentSel in listBoxSelections.SelectedItems)
            {
                if (currentSel.GetType() == typeof(SpectrogramPane.XSelectionGroup))
                {
                    MessageBox.Show("You cannot set the noise selection for a group", "Noise Selection", 
                        MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            SetNoiseWindow setWindow = new SetNoiseWindow(Spectrogram);

            setWindow.Selection = ((SpectrogramPane.XSelection)listBoxSelections.SelectedItem).NoiseSelection;

            if (setWindow.ShowDialog() == true)
            {
                foreach (SpectrogramPane.XSelection currentSel in listBoxSelections.SelectedItems)
                {
                    currentSel.NoiseSelection = setWindow.Selection;
                }

                listBoxSelections.ItemsSource = null;
                listBoxSelections.ItemsSource = Spectrogram.SelectedXPoints;
            }
        }
    }
}
